/*
 * LibraryDatabase.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.library;

import java.io.File;
import java.util.ArrayList;
import java.util.Properties;

import android.util.Log;
import de.studiorutton.access.FileLocation;
import de.studiorutton.access.Tools;
import de.studiorutton.config.ConfigProperties;
import de.studiorutton.gerengdic.LearningWordsApplication;

public class LibraryDatabase {
	private static final String TAG = "learningwords";
	private static LibraryDatabase INSTANCE = null;

	private ArrayList<FileLocation> fileLocations;
	
	public final static int DE_EN_DICT = 0;
	public final static int DE_STAT = 1;
	public final static int EN_STAT = 2;
	
	public LibraryDatabase() {
		fileLocations = new ArrayList<FileLocation>();
		fileLocations.add( createFileLocation( "de-en", "de-en.ndict", Tools.BINARY_CHARSET, true ));
		fileLocations.add( createFileLocation( "statistics", "top10000de.txt", Tools.ISO_8859_1, true ));
		fileLocations.add( createFileLocation( "statistics", "top10000en.txt", Tools.ISO_8859_1, true ));
//		Log.d( TAG, "Initialized LibraryLocations." );
	}
	
	public static LibraryDatabase get() {
		if ( INSTANCE == null )
			INSTANCE = new LibraryDatabase();
		return INSTANCE;
	}
	
	public static void release() {
		INSTANCE = null;
	}
	
	public FileLocation getFileLocation( int fileLocation ) {
		if ( fileLocation < 0 || fileLocation >= fileLocations.size())
			throw new IllegalArgumentException( "Index out of bounds." );
		return fileLocations.get( fileLocation );
	}
	
	protected FileLocation createFileLocation( String libraryName, String fileName, String charset, boolean compressed ) {
		FileLocation fileLocation = new FileLocation();
		Properties configProperties = LearningWordsApplication.getConfig().getProperties();
		fileLocation.baseURL = configProperties.getProperty( ConfigProperties.BASE_URL );
		String appName = configProperties.getProperty( ConfigProperties.APP_NAME);
		
		
		fileLocation.localPath = appName + File.separator + libraryName;
		fileLocation.remoteURIPath = appName + "/" + libraryName;
		fileLocation.fileName = fileName;
		fileLocation.charset = charset;
		
		if ( compressed )
			fileLocation.encodingSuffix = "gz";
		
		return fileLocation;

	}
	
}
